﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using SilverApp.View;
using System.ServiceModel;

namespace SilverApp.ViewModel
{
    public class FolderViewModel : ViewModelBase
    {
        private System.Guid                                 currentid;
        private FolderItemViewModel currentFolder;
        HomeViewModel                                       mparent;

        #region Folders Command
        private ICommand                                    enterFolderCommand;
        private ICommand                                    openFileCommand;
        private ICommand                                    reopenFileCommand;
        private ICommand                                    closeFileWinCommand;
        private ICommand                                    minimizeFileWinCommand;
        private ICommand                                    saveMiscFileCommand;
        private ICommand                                    cancelSaveMiscFileCommand;
        private ICommand                                    uploadFileCommand;
        private ICommand                                    cancelUploadFileCommand;
        private ICommand                                    showUploadFileCommand;
        private ICommand                                    showCreateFolderCommand;
        private ICommand                                    createFolderCommand;
        private ICommand goParentCommand;
        private ICommand removeFolderCommand;
        #endregion

        #region Collections
        private List<LiveService.Folder>                    folders;
        private List<LiveService.File>                      files;
        private ObservableCollection<FolderItemViewModel>   foldersCollection;
        private ObservableCollection<FileItemViewModel>     filesCollection;
        private List<FileWindowViewModel>                   fileWindows = new List<FileWindowViewModel>();
        private ObservableCollection<FileWindowViewModel>   filesWinCollection = new ObservableCollection<FileWindowViewModel>();
        #endregion

        #region Windows
        private FileWindowViewModel                         currentWindowOpened;
        private FileMiscWindow miscWin;
        private FileUploadWindow upWin;
        private CreateFolderWindow foldWin;
        #endregion

        private String folderName;
        public string webpath;
        private string mtest = "plop";

        public FolderViewModel()
        {
            ;
        }

        public FolderViewModel(HomeViewModel parent)
        {
            mparent = parent;
            currentid = mparent.CurrentComputer.Model.id;
            this.CurrentFolder = new FolderItemViewModel(mparent.CurrentComputer.Model, this);
            this.InitCommands();
            //this.InitSubFolders(mparent.CurrentComputer.Model.id);
            //this.InitSubFiles(mparent.CurrentComputer.Model.id);
            this.getServerWebPath();
            this.Debug = webpath;
            this.NextFolder(this.currentFolder);
        }

        private void InitCommands()
        {
            enterFolderCommand = new RelayCommand(NextFolder, CanNextFolder);
            openFileCommand = new RelayCommand(OpenFile, CanOpenFile);
            reopenFileCommand = new RelayCommand(ReopenFile, CanReopenFile);
            closeFileWinCommand = new RelayCommand(CloseFileWin, CanCloseFileWin);
            minimizeFileWinCommand = new RelayCommand(MinimizeFileWin, CanMinimizeFileWin);
            saveMiscFileCommand = new RelayCommand(SaveMiscFile, CanSaveMiscFile);
            cancelSaveMiscFileCommand = new RelayCommand(CancelSaveMiscFile, CanCancelSaveMiscFile);
            //uploadFileCommand = new RelayCommand(UploadFile, CanUploadFile);
            cancelUploadFileCommand = new RelayCommand(CancelUploadFile, CanCancelUploadFile); 
            showUploadFileCommand = new RelayCommand(ShowUploadFile, CanShowUploadFile);
            showCreateFolderCommand = new RelayCommand(ShowCreateFolder, CanShowCreateFolder);
            createFolderCommand = new RelayCommand(CreateFolder, CanCreateFolder);
           // removeFolderCommand = new RelayCommand(RemoveFolder, CanRemoveFolder);
            goParentCommand = new RelayCommand(GoParent, CanGoParent);
        }

        private FolderItemViewModel CurrentFolder
        {
            get { return this.currentFolder; }
            set { this.currentFolder = value; }
        }

        private void NextFolder(object parameter)
        {
            if (parameter != null)
            {
                FolderItemViewModel test = (FolderItemViewModel)parameter;
                mparent.RegisterCurrentFolder(test);
                this.CurrentFolder = test;
                this.currentid = test.Model.id;
                this.InitSubFolders(this.currentid);
                this.InitSubFiles(this.currentid);
                this.getServerWebPath();
                OnPropertyChanged("CurrentFolderPath");
                OnPropertyChanged("FoldersCollection");
                OnPropertyChanged("FilesCollection");
            }
            else { Debug = "next pas settes"; }
        }

        private bool CanNextFolder(object parameter)
        {
            return true;
        }

        private bool CanOpenFile(object parameter)
        {
            return true;
        }

        private void OpenFile(object parameter)
        {
            try
            {
                if (parameter != null)
                {
                    FileItemViewModel tmp = (FileItemViewModel)parameter;
                    int flag = 0;
                    foreach (FileWindowViewModel vm in this.FilesWinCollection)
                    {
                        if (vm.item.Model.id == tmp.Model.id)
                        {
                            this.currentWindowOpened = new FileWindowViewModel(this, vm.view, vm.item);
                            vm.view.Show();
                            flag = 1;
                            break;
                        }
                    } 
                    if (flag == 0)
                    {
                        FileWindow child = new FileWindow();
                        child.DataContext = this;
                        this.currentWindowOpened = new FileWindowViewModel(this, child, tmp);
                        if (this.isMediaImage(tmp.Model.extension) == true)
                            this.OpenImageFile(child);
                        else if (this.isMediaVideo(tmp.Model.extension) == true)
                            this.OpenVideoFile(child);
                        else if (this.isMediaMusic(tmp.Model.extension) == true)
                            this.OpenMusicFile(child);
                        else if (this.isMediaText(tmp.Model.extension) == true)
                            this.OpenTextFile();
                        else
                            this.OpenMiscFile();
                    }
                }
            }
            catch (Exception ex) { this.Debug = ex.Message; }
        }

        public FileWindow tchild;

        #region Open Files methods

        private SaveFileDialog mdialog;

        private void OpenMiscFile()
        {
            try
            {
                mdialog = new SaveFileDialog();
                mdialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";
                mdialog.FilterIndex = 2;
                miscWin = new FileMiscWindow(this);
                miscWin.Show();
            }
            catch (Exception ex) { this.Debug = "Open Misc : " + ex.Message; }
        }

        private void SaveMiscFile(object parameter)
        {
            try
            {
                bool? dialogResult = mdialog.ShowDialog();

                if (dialogResult == true)
                {
                    miscWin.Close();
                    try
                    {
                        this.Debug = "Save Misc : " + "Getting file from the server...";
                        this.Sclient.getServerFileCompleted += new EventHandler<LiveService.getServerFileCompletedEventArgs>(getServerFileCompleted);
                        this.Sclient.getServerFileAsync(this.Parent.CurrentUser.login, this.currentid, "test");
                    }
                    catch (Exception ex) { this.Debug = "Save Misc : " + ex.Message; }
                }
            }
            catch (Exception ex) { this.Debug = "Show dial Misc : " + ex.Message; }
        }

        private bool CanSaveMiscFile(object parameter)
        {
            return true;
        }

        private void CancelSaveMiscFile(object parameter)
        {
            try { this.miscWin.Close(); }
            catch (Exception ex) { this.Debug = ex.Message; }
        }

        private bool CanCancelSaveMiscFile(object parameter)
        {
            return true;
        }

        private void getServerFileCompleted(object sender, LiveService.getServerFileCompletedEventArgs e)
        {
            try
            {
              byte[] fileBytes = e.Result as byte[];
              using (Stream fs = (Stream) mdialog.OpenFile())
                {
                    fs.Write(fileBytes, 0, fileBytes.Length);
                    fs.Close();
                    Debug = "File successfully saved!";
                    this.Alert("File successfully saved!");
                }
            }
            catch (Exception ex) { Debug = "File completed : " + ex.Message; } 
        }
       //  ---
        private void ShowUploadFile(object parameter)
        {
            try
            {
                upWin = new FileUploadWindow(this);
                upWin.Show();
            }
            catch (Exception ex) { this.Debug = "Open Misc : " + ex.Message; }
        }

        private bool CanShowUploadFile(object parameter)
        {
            return true;
        }

        /* parentid 
         */

       /* public void UploadFile(byte[] datas, LiveService.File info)
        {
          try
            {
                upWin.Close();
                try
                {
                    this.Debug = "Upload : " + "sending file from the server... etxension = " + info.extension;
                    this.Sclient.createUploadedFileCompleted += new EventHandler<LiveService.createUploadedFileCompletedEventArgs>(getLoadServerFileCompleted);
                    this.Sclient.createUploadedFileAsync(this.currentid, info, datas, mparent.CurrentUser.id);
                }
                catch (Exception ex) { this.Debug = "Upload X: " + ex.Message; }
                //}
            }
            catch (Exception ex) { this.Debug = "Upload : " + ex.Message; }
        }*/


        public void UploadFile(byte[] datas, LiveService.File info)
        {
            try
            {
                FileTransfert.ChunkSender sender = new FileTransfert.ChunkSender(this.currentid, info, mparent.CurrentUser.id);
                sender.InitializeChunkList(datas);
                Debug = "Nb chunks : " + sender.NbChunks.ToString() ;
                if (sender.NbChunks > 1000)
                {
                   // this.Alert("Specified file exceeds current MLM version max uplaod size.");
                   Debug = "Specified file exceeds current MLM version max uplaod size.";

                }
                sender.UpWin = this.upWin;
                sender.UploadChunk();
                this.InitSubFiles(this.currentid);
            }
            catch (Exception ex) { this.Debug = "Upload failed : " + ex.Message; }
        }

        private void getLoadServerFileCompleted(object sender, LiveService.createUploadedFileCompletedEventArgs e)
        {
            try
            {
                if (e.Result == true)
                {
                    Debug = "File successfully saved!";
                    this.InitSubFiles(currentid);
                    this.Alert("File successfully loaded !"); 
                }
                else
                {
                    Debug += "erreur !";
                    this.Sclient.getLastErrorCompleted += new EventHandler<LiveService.getLastErrorCompletedEventArgs>(Sclient_getLastErrorCompleted);
                    this.Sclient.getLastErrorAsync();
                } 
            }
            catch (Exception ex)
            {
                this.Sclient.getLastErrorCompleted += new EventHandler<LiveService.getLastErrorCompletedEventArgs>(Sclient_getLastErrorCompleted);
                this.Sclient.getLastErrorAsync();
                Debug += " toto File completed : " + ex.Message + ex.Data + ex.InnerException;
            }
        }

        private void Sclient_getLastErrorCompleted(object sender, LiveService.getLastErrorCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                if (e.Result.Length <= 0)
                    Debug = "result <= 0";
                Debug = e.Result;
            }
        }

        private bool CanUploadFile(object parameter)
        {
            return true;
        }

        private void CancelUploadFile(object parameter)
        {
            try { this.upWin.Close(); }
            catch (Exception ex) { this.Debug = ex.Message; }
        }

        private bool CanCancelUploadFile(object parameter)
        {
            return true;
        }

        // ----

        private void getServerWebPath()
        {
            try
            {
                this.Sclient.getWebFilePathCompleted += new EventHandler<LiveService.getWebFilePathCompletedEventArgs>(setWebPath);
                this.Sclient.getWebFilePathAsync(this.currentid);
            }
            catch (Exception ex) { this.Debug = "Upload X: " + ex.Message; }
        }

        private void setWebPath(object sender, LiveService.getWebFilePathCompletedEventArgs e)
        {
            this.webpath = e.Result;
        }

        // Open a file as image
        private void OpenImageFile(FileWindow child)
        {
            try
            {
                Image p = new Image();
                p.MaxHeight = 900;
                p.MaxWidth = 1400;
                ImageSource source = new BitmapImage(new Uri("http://192.168.0.12/MyLiveMesh/Clientbin/" + this.webpath + this.CurrentOpenedWindow.item.ModelName, UriKind.RelativeOrAbsolute));
                p.Source = source;
                child.CMedia.Children.Add(p);
                child.Show();
                this.FilesWinCollection.Add(this.currentWindowOpened);
                Debug = this.webpath + this.CurrentOpenedWindow.item.ModelName;
            }
            catch (Exception ex) { this.Debug = ex.Message; }
        }

        private void OpenVideoFile(FileWindow child)
        {
            try
            {
                Microsoft.SilverlightMediaFramework.Core.SMFPlayer vplayer = new Microsoft.SilverlightMediaFramework.Core.SMFPlayer();
                Microsoft.SilverlightMediaFramework.Core.Media.PlaylistItem item = new Microsoft.SilverlightMediaFramework.Core.Media.PlaylistItem();
                Debug = this.webpath + this.CurrentOpenedWindow.item.ModelName;
                item.MediaSource = new Uri("http://192.168.0.12/MyLiveMesh/Clientbin/" + this.webpath + this.CurrentOpenedWindow.item.ModelName, UriKind.RelativeOrAbsolute);
                vplayer.Playlist.Add(item);
                vplayer.Height = 400;
                child.CMedia.Children.Add(vplayer);
                child.Show();
                vplayer.GoToPlaylistItem(item);
                this.FilesWinCollection.Add(this.currentWindowOpened);
            }
            catch (Exception ex) { this.Debug = ex.Message; }
        }

        private void OpenMusicFile(FileWindow child)
        {
            try
            {
                Microsoft.SilverlightMediaFramework.Core.SMFPlayer vplayer = new Microsoft.SilverlightMediaFramework.Core.SMFPlayer();
                Microsoft.SilverlightMediaFramework.Core.Media.PlaylistItem item = new Microsoft.SilverlightMediaFramework.Core.Media.PlaylistItem();
                Debug = this.webpath + this.CurrentOpenedWindow.item.ModelName;
                item.MediaSource = new Uri("http://192.168.0.12/MyLiveMesh/Clientbin/" + this.webpath + this.CurrentOpenedWindow.item.ModelName, UriKind.RelativeOrAbsolute);
                vplayer.Playlist.Add(item);//Insert(0, item);
                vplayer.Height = 40;
                child.CMedia.Children.Add(vplayer);
                child.Show();
                vplayer.GoToPlaylistItem(item);
                this.FilesWinCollection.Add(this.currentWindowOpened);
            }
            catch (Exception ex) { this.Debug = ex.Message; }
        }

        private void OpenTextFile()
        {
            try
            {
                Debug = this.webpath + this.CurrentOpenedWindow.item.ModelName;
                this.Sclient.getServerFileCompleted += new EventHandler<LiveService.getServerFileCompletedEventArgs>(getServerFileContentCompleted);
                this.Sclient.getServerFileAsync(this.Parent.CurrentUser.login, this.currentid, this.CurrentMediaSource);
            }
            catch (Exception ex) { this.Debug = "Show dial Misc : " + ex.Message; }
        }

        private void getServerFileContentCompleted(object sender, LiveService.getServerFileCompletedEventArgs e)
        {
            try
            {
                if (e.Result.Length > 0)
                {
                    byte[] fileBytes = e.Result as byte[];

                    TextBlock text = new TextBlock();
                    text.TextWrapping = TextWrapping.Wrap;
                    text.Text = System.Text.Encoding.UTF8.GetString(fileBytes, 0, fileBytes.Length);
                    if (this.CurrentOpenedWindow != null)
                    {
                        if (this.CurrentOpenedWindow.view != null)
                        {
                            this.CurrentOpenedWindow.view.CMedia.Children.Add(text);
                            this.CurrentOpenedWindow.view.Show();
                            this.FilesWinCollection.Add(this.currentWindowOpened);
                        }
                        else { Debug = "Text opener : current winodw viwe null "; }
                    }
                    else { Debug = "Text opener : current winodw null "; }
                }
                else { Debug = "Text opener : result vide "; }
            }
            catch (Exception ex) { Debug = "Text opener : " + ex.Message; }
        }

        #endregion

        private bool CanReopenFile(object parameter)
        {
            return true;
        }

        private void ReopenFile(object parameter)
        {
            if (parameter != null)
            {
                if (this.CurrentOpenedWindow != null)
                    this.CloseFileWin(parameter);
                FileWindowViewModel tmp = (FileWindowViewModel)parameter;
                foreach (FileWindowViewModel vm in this.FilesWinCollection)
                {
                    if (vm.item.Model.id == tmp.item.Model.id)
                    {
                        this.currentWindowOpened = new FileWindowViewModel(this, vm.view, vm.item);
                        vm.view.Show();
                        break;
                    }
                }
            }
        }

        private void CloseFileWin(object parameter)
        {
            this.currentWindowOpened.view.Close();
            int i = 0;
            foreach (FileWindowViewModel vm in this.FilesWinCollection)
            {
                if (vm.item.Model.id == currentWindowOpened.item.Model.id)
                    break;
                else
                    i++;
            }

            this.currentWindowOpened = null;
            this.FilesWinCollection.RemoveAt(i);
        }

        private bool CanCloseFileWin(object parameter)
        {
            return true;
        }

        private void MinimizeFileWin(object parameter)
        {
            this.currentWindowOpened.view.Close();
            this.CurrentOpenedWindow = null;
        }

        private bool CanMinimizeFileWin(object parameter)
        {
            return true;
        }

        private void GoParent(object parameter)
        {
            if (this.CurrentFolder != null)
            {
                if (this.CurrentFolder.Model != null)
                {
                    this.Sclient.getFolderByIdCompleted += new EventHandler<LiveService.getFolderByIdCompletedEventArgs>(getParentFolderCompleted);
                    this.Sclient.getFolderByIdAsync(this.currentFolder.Model.id);
                }
            }
        }

        private void getParentFolderCompleted(object sender, LiveService.getFolderByIdCompletedEventArgs e)
        {

            if (e.Result != null)
            {
                LiveService.Folder pfold = e.Result;
                if (pfold.foldertype == 0)
                    this.NextFolder(new FolderItemViewModel(pfold, this));
                else
                {
                    this.mparent.RegisterCurrentFolder(new FolderItemViewModel(pfold, this));
                    this.mparent.RegisterCurrentComputer(new ComputerItemViewModel(pfold, new ComputerViewModel(this.mparent)));
                    this.mparent.RegisterNextView("Computer");
                }
            }
        }

        private bool CanGoParent(object parameter)
        {
            if (this.CurrentFolder != null) 
            {
                if (this.CurrentFolder.Model != null)
                {
                        return true;
                }
            }
            return false;
        }

        public String CurrentFolderPath
        {
            get { return "[ current folder ][>] " + this.CurrentFolder.Model.name; }
        }

        #region File extension test
        private bool isMediaVideo(String extension)
        {
            bool result = false;
            switch (extension.Trim())
            {
                case ".wmv":
                case ".avi":
                    result = true;
                    break;

                default:
                    break;
            }
            return result;
        }

        private bool isMediaText(String extension)
        {
            return ((extension.Trim() == ".txt" || extension.Trim() == ".TXT") ? true : false);
        }

        private bool isMediaImage(String extension)
        {
            bool result = false;
            switch (extension.Trim())
            {
                case ".JPG":
                case ".JPEG":
                case ".jpg":
                case ".jpeg":
                    result = true;
                    break;

                case ".PNG":
                case ".png":
                    result = true;
                    break;

                default:
                    break;
            }
            return result;
        }

        private bool isMediaMusic(String extension)
        {
            bool result = false;
            switch (extension.Trim())
            {
                case ".wma":
                case ".mp3":
                    result = true;
                    break;

                default:
                    break;
            }
            return result;
        }

        #endregion

        #region Folder creation 
        public String FolderName
        {
            get { return this.folderName; }
            set { this.folderName = value; }
        }

        private void ShowCreateFolder(object parameter)
        {
            try
            {
                foldWin = new CreateFolderWindow(this);
                foldWin.Show();
            }
            catch (Exception ex) { this.Debug = "Open Misc : " + ex.Message; }
        }

        private bool CanShowCreateFolder(object parameter)
        {
            return true;
        }

        private void CreateFolder(object parameter)
        {
            try
            {
                int flag = 0;
                foreach (FolderItemViewModel tmp in this.FoldersCollection)
                {
                    if (tmp.Model.name.Trim() == FolderName.Trim())
                        flag = 1;
                }
                if (flag == 0)
                {
                    LiveService.Folder newFold = new LiveService.Folder();
                    newFold.id = System.Guid.NewGuid();
                    newFold.internalpath = "userpath";
                    newFold.iconpath = "iconpath";
                    newFold.lastmodified = System.DateTime.Now;
                    newFold.name = FolderName;
                    newFold.ownerid = mparent.CurrentUser.id;
                    newFold.parentid = currentid;
                    newFold.foldertype = 0;
                    this.Sclient.createNewFolderCompleted += new EventHandler<LiveService.createNewFolderCompletedEventArgs>(CreateFolderCompleted);
                    this.Sclient.createNewFolderAsync(mparent.CurrentUser, newFold);
                }
                else
                {
                    Debug = "Existing folder ! Please choose another name";
                }
            }
            catch (Exception ex) { this.Debug = "Create folder : currentid == " + FolderName + " " + ex.Message; }
        }

        private bool CanCreateFolder(object parameter)
        {
            return true;
        }

        public void CreateFolderCompleted(object sender, LiveService.createNewFolderCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                Debug = "erreur";
            }
            else
            {
                this.foldWin.Close();
                InitSubFolders(currentid);
                Debug = "Folder Created";
            }
        }
        
        private void RemoveFolder(object parameter)
        {
            try
            {
                FolderItemViewModel p = (FolderItemViewModel) parameter;
                this.Sclient.removeFolderCompleted += new EventHandler<LiveService.removeFolderCompletedEventArgs>(removeFolderCompleted);
                this.Sclient.removeFolderAsync(p.Model.id);
            }
            catch (Exception ex) { this.Debug = "remove folder : currentid == " + FolderName + " " + ex.Message; }
        }

       private bool CanRemoveFolder(object parameter)
        {
            return true;
        }

        public void removeFolderCompleted(object sender, LiveService.removeFolderCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                Debug = "erreur";
            }
            else
            {
                AlarmWindow test = new AlarmWindow();
                TextBlock pmsg = new TextBlock();
                pmsg.Text = "Folder removed !";
                test.LayoutRoot.Children.Add(pmsg);
                test.Show();
                InitSubFolders(this.currentid);
                OnPropertyChanged("FoldersCollections");
            }
        }
        
        #endregion

        #region Members and Current * Accessors

        public HomeViewModel Parent
        {
            get { return this.mparent; }
        }

        private String getSubdirectoryByExt(string extension)
        {
            String res = " ";
            switch (extension.Trim())
            {
                case "wmv":
                    res = "videos";
                    break;

                case "txt":
                    res = "text";
                    break;

                case "png":
                case "jpg":
                case "jpeg":
                case "PNG":
                case "JPG":
                case "JPEG":
                    res = "images";
                    break;

                case "mp3":
                case "wma":
                    res = "musics";
                    break;

                default:
                    res = "misc";
                    break;
            }
            return res;
        }

        public String CurrentMediaSource
        {
            get { return this.CurrentOpenedWindow.item.Model.name.Trim(); }
        }

        public String CurrentMediaSourcePath
        {
            get { return "/Files/" + this.mparent.CurrentUser.login.Trim() + "/" + this.getSubdirectoryByExt(this.CurrentOpenedWindow.item.Model.extension.Trim()) + "/" + this.CurrentOpenedWindow.item.Model.name.Trim(); }
        }

        public String CurrentOpenedMediaSource
        {
            get { return this.CurrentOpenedWindow.item.Model.internalpath; }
        }

        public String CurrentOpenedMediaExtension
        {
            get { return this.CurrentOpenedWindow.item.Model.extension; }
        }

        public FileWindowViewModel CurrentOpenedWindow
        {
            get { return this.currentWindowOpened; }
            set { this.currentWindowOpened = value; }
        }

        #endregion

        #region Commands getters/setters
        public ICommand EnterFolderCommand
        {
            get { return this.enterFolderCommand; }
            set { this.enterFolderCommand = value; }
        }

        public ICommand OpenFileCommand
        {
            get { return this.openFileCommand; }
            set { this.openFileCommand = value; }
        }

        public ICommand ReopenFileCommand
        {
            get { return this.reopenFileCommand; }
            set { this.reopenFileCommand = value; }
        }

        public ICommand CloseFileWinCommand
        {
            get { return this.closeFileWinCommand; }
            set { this.closeFileWinCommand = value; }
        }

        public ICommand MinimizeFileWinCommand
        {
            get { return this.minimizeFileWinCommand; }
            set { this.minimizeFileWinCommand = value; }
        }

        public ICommand SaveMiscFileCommand
        {
            get { return this.saveMiscFileCommand; }
            set { this.saveMiscFileCommand = value; }
        }

        public ICommand CancelSaveMiscFileCommand
        {
            get { return this.cancelSaveMiscFileCommand; }
            set { this.cancelSaveMiscFileCommand = value; }
        }

        public ICommand UploadFileCommand
        {
            get { return this.uploadFileCommand; }
            set { this.uploadFileCommand = value; }
        }

        public ICommand CancelUploadFileCommand
        {
            get { return this.cancelUploadFileCommand; }
            set { this.cancelUploadFileCommand = value; }
        }

        public ICommand ShowUploadFileCommand
        {
            get { return this.showUploadFileCommand; }
            set { this.showUploadFileCommand = value; }
        }


        public ICommand ShowCreateFolderCommand
        {
            get { return this.showCreateFolderCommand; }
            set { this.showCreateFolderCommand = value; }
        }

        public ICommand CreateFolderCommand
        {
            get { return this.createFolderCommand; }
            set { this.createFolderCommand = value; }
        }

        public ICommand RemoveFolderCommand
        {
            get { return this.removeFolderCommand; }
            set { this.removeFolderCommand = value; }
        }

        public ICommand GoParentCommand
        {
            get { return this.goParentCommand; }
            set { this.goParentCommand = value; }
        }

        #endregion

        public void InitSubFolders(System.Guid parentid)
        {
            try
            {
                this.Sclient.getFolderFoldersCompleted += new EventHandler<LiveService.getFolderFoldersCompletedEventArgs>(InitSubFoldersCompleted);
                this.Sclient.getFolderFoldersAsync(parentid);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void InitSubFoldersCompleted(object sender, LiveService.getFolderFoldersCompletedEventArgs e)
        {
            this.folders = e.Result;
            ObservableCollection<FolderItemViewModel> tmpcol = new ObservableCollection<FolderItemViewModel>();
            foreach (LiveService.Folder xmp in this.folders)
            {
               tmpcol.Add(new FolderItemViewModel(xmp, this));
            }
            this.FoldersCollection = tmpcol;
        }

        public ObservableCollection<FolderItemViewModel> FoldersCollection
        {
            get { return foldersCollection; }
            set
            {
                if (foldersCollection != value)
                {
                    foldersCollection = value;
                    OnPropertyChanged("FoldersCollection");
                }
            }
        }

        public void InitSubFiles(System.Guid parentid)
        {
            try
            {
                this.Sclient.getFolderFilesCompleted += new EventHandler<LiveService.getFolderFilesCompletedEventArgs>(InitSubFilesCompleted);
                this.Sclient.getFolderFilesAsync(parentid);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void InitSubFilesCompleted(object sender, LiveService.getFolderFilesCompletedEventArgs e)
        {
            this.files = e.Result;
            ObservableCollection<FileItemViewModel> tmpcol = new ObservableCollection<FileItemViewModel>();
            foreach (LiveService.File xmp in this.files)
            {
                tmpcol.Add(new FileItemViewModel(xmp, this));
            }
            this.FilesCollection = tmpcol;
        }

        public ObservableCollection<FileItemViewModel> FilesCollection
        {
            get { return filesCollection; }
            set
            {
                if (filesCollection != value)
                {
                    filesCollection = value;
                    OnPropertyChanged("FilesCollection");
                }
            }
        }

        public ObservableCollection<FileWindowViewModel> FilesWinCollection
        {
            get { return filesWinCollection; }
            set
            {
                if (filesWinCollection != value)
                {
                    filesWinCollection = value;
                    OnPropertyChanged("FilesWinCollection");
                }
            }
        }

        public String Test
        {
            get { return "plop"; }
            set { mtest = value;  OnPropertyChanged("Test"); }
        }

        public String exmessage = "no error";
        public String Debug
        {
            get { return exmessage; }
            set {  exmessage = value;  OnPropertyChanged("Debug"); }
        }
    }
}
